package lc;

import java.util.*;

/**
 * 46全排列
 */
public class QuanPaiLie46 {
    public List<List<Integer>> permute(int[] nums) {
        int len = nums.length;
        List<List<Integer>> res = new ArrayList<>();
        if (len == 0) {
            return res;
        }
        boolean[] used = new boolean[len];
        Deque<Integer> path = new ArrayDeque<>();
        dfs(nums, len, 0, path, used, res);
        return res;
    }

    private void dfs(int[] nums, int len, int depth, Deque<Integer> path, boolean[] used, List<List<Integer>> res) {
        if (depth == len) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < len; i++) {
            if (!used[i]) {
                path.add(nums[i]);
                used[i] = true;
                System.out.println("   递归之前 =>  "+path);
                dfs(nums, len, depth+1, path, used, res);
                used[i] = false;
                path.removeLast();
                System.out.println("   递归之后 =>  "+ path);
            }
        }
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        QuanPaiLie46 qpl = new QuanPaiLie46();
        List<List<Integer>> lists = qpl.permute(nums);
        System.out.println(lists);
    }
}
